<template>
  <el-dialog
    :model-value="dialogVisible"
      :title="dialogTitle"
    width="40%"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="form" label-width="70px" :rules="rules">
      <el-form-item label="用户名" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="登录名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="dialogTitle === '添加用户'">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="mobile">
        <el-input v-model="form.mobile"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">保存</el-button
        >
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
  import { defineEmits, ref, defineProps, watch } from 'vue'
  import { postRequest, putRequest } from '@/api/request'
  import { useI18n } from 'vue-i18n'
  import { ElMessage } from 'element-plus'

  const i18n = useI18n()

  const props = defineProps({
    dialogTitle: {
      type: String,
      default: '',
      required: true
    },
    dialogTableValue: {
      type: Object,
      default: () => {}
    }
  })

  const formRef = ref(null)

  const form = ref({
    name: '',
    username: '',
    password: '',
    mobile: '',
    enabled: false
  })

  watch(() => props.dialogTableValue, () => {
    console.log(props.dialogTableValue)
    form.value = props.dialogTableValue
  }, { deep: true, immediate: true })

  const emits = defineEmits(['update:modelValue', 'initUserList'])

  const handleClose = () => {
    emits('update:modelValue', false)
  }

  // 保存用户
  const handleConfirm = () => {
    formRef.value.validate(async (valid) => {
          if (valid) {
            props.dialogTitle === '添加用户' ? await putRequest('/user', form.value) : postRequest('/user', form.value)
            ElMessage({
              message: i18n.t('message.updeteSuccess'),
              type: 'success'
            })
            emits('initUserList')
            handleClose()
          } else {
            console.log('error submit!')
            return false
          }
      }
    )
  }

  const rules = ref({
    name: [
      {
        required: true,
        message: 'Please input Activity name',
        trigger: 'blur'
      }
    ],
    username: [
      {
        required: true,
        message: 'Please input Activity name',
        trigger: 'blur'
      }
    ],
    password: [
      {
        required: true,
        message: 'Please input Activity name',
        trigger: 'blur'
      }
    ],
    mobile: [
      {
        required: true,
        message: 'Please input Activity name',
        trigger: 'blur'
      }
    ]
  })

</script>

<style scoped>

</style>
